package cn.zwx.activiti.example.controller;

import cn.zwx.activiti.example.common.ResponseEntry;
import cn.zwx.activiti.example.model.req.*;
import cn.zwx.activiti.example.model.resp.QueryInstancesListResp;
import cn.zwx.activiti.example.model.resp.QueryVariablesResp;
import cn.zwx.activiti.example.model.resp.StartProcessResp;
import cn.zwx.activiti.example.service.ProcessInstanceService;
import cn.zwx.activiti.example.utils.SecurityUtil;
import cn.zwx.activiti.example.utils.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.api.runtime.shared.query.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @projectName:activiti-example
 * @see:cn.zwx.activiti.example.controller
 * @author:zhangwenxue
 * @createTime:2021/5/30 0:01
 * @version:1.0
 */
@RestController
@RequestMapping("process/instance")
@Api(tags = "工作流实例Api")
public class ProcessInstanceController extends BaseController{

    @Autowired
    private SecurityUtil securityUtil;

    @Autowired
    private ProcessInstanceService processInstanceService;


    /**
     * @description 获取流程实例列表
     * @return cn.zwx.activiti.example.common.ResponseEntry<org.activiti.api.runtime.shared.query.Page<QueryInstancesListResp>>
     * @author zhangwenxue
     * @createTime 2021/6/1 23:18
     **/
    @ApiOperation("获取流程实例列表")
    @RequestMapping(value = "/getInstances",method = RequestMethod.POST)
    public ResponseEntry<Page<QueryInstancesListResp>> getInstancesList(@RequestBody QueryInstancesListReq req) {
        // 校验参数
        securityUtil.logInAs(this.getUserName());
        ValidatorUtils.getInstance().validateEntity(req);
        // 请求接口 instanceVariable
        Page<QueryInstancesListResp> page  = processInstanceService.getInstancesList(req);
        // 返回参数
        return ResponseEntry.ok(page);
    }

    /**
     * @description 启动流程实例
     * @return cn.zwx.activiti.example.common.ResponseEntry<cn.zwx.activiti.example.model.resp.StartProcessResp>
     * @author zhangwenxue
     * @createTime 2021/6/1 22:03
     **/
    @RequestMapping(value = "/startProcess",method = RequestMethod.POST)
    @ApiOperation("启动流程实例")
    public ResponseEntry<StartProcessResp> startProcess(@RequestBody StartProcessReq req) {
        // 校验参数
        securityUtil.logInAs(this.getUserName());
        ValidatorUtils.getInstance().validateEntity(req);
        // 请求接口 instanceVariable
        StartProcessResp resp  = processInstanceService.startProcess(req);
        // 返回参数
        return ResponseEntry.ok(resp);
    }

    /**
     * @description 流程挂起
     * @return cn.zwx.activiti.example.common.ResponseEntry<java.lang.Void>
     * @author zhangwenxue
     * @createTime 2021/6/2 20:30
     **/
    @ApiOperation("流程挂起")
    @RequestMapping(value = "/suspendInstance",method = RequestMethod.POST)
    public ResponseEntry<Void> suspendInstance(@RequestBody SuspendInstanceReq req) {
        // 校验参数
        securityUtil.logInAs(this.getUserName());
        ValidatorUtils.getInstance().validateEntity(req);
        // 请求接口 instanceVariable
        processInstanceService.suspendInstance(req);
        // 返回参数
        return ResponseEntry.ok();
    }


    /**
     * @description 流程激活
     * @return cn.zwx.activiti.example.common.ResponseEntry<java.lang.Void>
     * @author zhangwenxue
     * @createTime 2021/6/2 20:30
     **/
    @ApiOperation("流程激活")
    @RequestMapping(value = "/resumeInstance",method = RequestMethod.POST)
    public ResponseEntry<Void> resumeInstance(@RequestBody ResumeInstanceReq req) {
        // 校验参数
        securityUtil.logInAs(this.getUserName());
        ValidatorUtils.getInstance().validateEntity(req);
        // 请求接口 instanceVariable
        processInstanceService.resumeInstance(req);
        // 返回参数
        return ResponseEntry.ok();
    }


   /**
    * @description 获取流程实例参数
    * @return cn.zwx.activiti.example.common.ResponseEntry
    * @author zhangwenxue
    * @createTime 2021/6/2 20:44
    **/
    @ApiOperation("获取流程实例参数")
    @RequestMapping(value = "/variables",method =RequestMethod.POST)
    public ResponseEntry<List<QueryVariablesResp>> variables(@RequestBody QueryVariablesReq req) {
        // 校验参数
        securityUtil.logInAs(this.getUserName());
        ValidatorUtils.getInstance().validateEntity(req);
        // 请求接口 instanceVariable
        List<QueryVariablesResp> respList = processInstanceService.variables(req);
        // 返回参数
        return ResponseEntry.ok(respList);
    }

    /**
     * @description 删除流程实例
     * @return cn.zwx.activiti.example.common.ResponseEntry<java.lang.Void>
     * @author zhangwenxue
     * @createTime 2021/6/2 21:02
     **/
    @ApiOperation("删除流程实例")
    @RequestMapping(value = "/deleteInstance",method = RequestMethod.POST)
    public ResponseEntry<Void> deleteInstance(@RequestBody DeleteInstanceReq req) {
        // 校验参数
        securityUtil.logInAs(this.getUserName());
        ValidatorUtils.getInstance().validateEntity(req);
        // 请求接口 instanceVariable
        processInstanceService.deleteInstance(req);
        // 返回参数
        return ResponseEntry.ok();
    }



}
